package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"github.com/jinzhu/gorm"
	"huiyuan/models"
)

type HuiYuanController struct {
	beego.Controller
}

type ReturnMsg struct {
	Cre  int
	Msg  string
	Data interface{}
}

func (c *HuiYuanController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	HuiYuanBqi := c.GetString("HuiYuanBqi")
	var data []models.HueYuan

	models.DB.Model(&data).Order("pai_xu desc").Where("hui_yuan_bqi like ? ",
		"%"+HuiYuanBqi+"%").Find(&data)
	c.Data["json"] = ReturnMsg{200, "查询成功", data}
	c.ServeJSON(true)
}

func (c *HuiYuanController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	HuiYuanBqi := c.GetString("HuiYuanBqi")
	PaiXu, _ := c.GetInt("PaiXu")
	HuiYuanNum, _ := c.GetInt("HuiYuanNum")
	BeiZhu := c.GetString("BeiZhu")
	data := models.HueYuan{
		Model:      gorm.Model{},
		HuiYuanBqi: HuiYuanBqi,
		PaiXu:      PaiXu,
		HuiYuanNum: HuiYuanNum,
		BeiZhu:     BeiZhu,
	}
	models.DB.Model(&data).Create(&data)
	c.Data["json"] = ReturnMsg{200, "新增成功", nil}
	c.ServeJSON(true)
}

func (c *HuiYuanController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	data := models.HueYuan{
		Model: gorm.Model{ID: uint(id)},
	}
	models.DB.Model(&data).Delete(&data)
	c.Data["json"] = ReturnMsg{200, "删除成功", data}
	c.ServeJSON(true)
}
